package com.faxsun.profile.core.dao;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.broadleafcommerce.common.persistence.EntityConfiguration;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.hibernate.ejb.QueryHints;
import org.springframework.stereotype.Repository;

import com.faxsun.profile.core.domain.FSReceiver;
import com.faxsun.profile.core.domain.FSReceiverImpl;

@Repository("receiverDao")
public class FSReceiverDaoImpl implements FSReceiverDao {

	@PersistenceContext(unitName = "blPU")
	protected EntityManager em;

	@Resource(name = "blEntityConfiguration")
	protected EntityConfiguration entityConfiguration;

	@Override
	public FSReceiver create() {
		FSReceiver receiver = (FSReceiver) entityConfiguration
				.createEntityInstance(FSReceiver.class.getName());
		return receiver;
	}

	@Override
	public FSReceiver save(FSReceiver receiver) {
		return em.merge(receiver);
	}

	@Override
	public void delete(FSReceiver receiver) {
		if (!em.contains(receiver)) {
			receiver = readById(receiver.getId());
		}
		em.remove(receiver);

	}

	@Override
	public FSReceiver readByIDCard(String idCard) {

		TypedQuery<FSReceiver> query = em.createNamedQuery(
				"FS_READ_IDINFO_BY_IDCARD", FSReceiver.class);
		query.setParameter("idCard", idCard);
		query.setHint(QueryHints.HINT_CACHEABLE, true);
		query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order");

		List<FSReceiver> results = query.getResultList();

		return results == null || results.isEmpty() ? null : results.get(0);
	}

	@Override
	public FSReceiver readById(long id) {
		// TODO Auto-generated method stub
		return em.find(FSReceiverImpl.class, id);
	}

	@Override
	public List<FSReceiver> readByCustomer(Customer customer) {
		Query query = em
				.createQuery("SELECT fsReceiver FROM com.faxsun.profile.core.domain.FSReceiver fsReceiver"
						+ " where fsReceiver.customer=:customer");
		query.setParameter("customer", customer);
		query.setHint(QueryHints.HINT_CACHEABLE, true);
		query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSReceiver");

		@SuppressWarnings("unchecked")
		List<FSReceiver> results = query.getResultList();

		return results;
	}

	@Override
	public List<FSReceiver> readByEntity(FSReceiver receiver) {
		Query query = em.createQuery("SELECT fsReceiver FROM com.faxsun.profile.core.domain.FSReceiver fsReceiver"
				+ " where fsReceiver.customer=:customer"
				+ " and fsReceiver.name=:name"
				+ " and fsReceiver.idCard=:idCard");
		query.setParameter("customer", receiver.getCustomer());
		query.setParameter("name", receiver.getName());
		query.setParameter("idCard", receiver.getIdCard());
		query.setHint(QueryHints.HINT_CACHEABLE, true);
		query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSReceiver");

		@SuppressWarnings("unchecked")
		List<FSReceiver> results = query.getResultList();
		return results;
	}

}
